﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PFMHandlers;
using PFMConfigurations;
using PFMCandidate;
using PFMFinders;
using PFMGlobals;

namespace PFMKernels
{
    public class PFMTask:ITask

    {
        protected List<IHandler> o_Handlers = null;
        public virtual bool Execute(PFMConfigurations.Schedule aSchedule, PFMConfigurations.Target aTarget)
        {
            return true;
        }
        protected virtual List<IHandler> CreateHandlersFromTarget(Target aTarget)
        {
            List<IHandler> result = new List<IHandler>();
            foreach (string s in aTarget.Handler)
            {
                result.Add(PFMHandlerFactory.CreateHandler(s));
            }
            result.Add(PFMHandlerFactory.CreateHandler(aTarget.Destination));
            return result;
        }
        //依据 Target 对象的 Handler 属性及 Destination 属性创建所有的 Handler 并按照次 序放在 List 中返回。
        protected void BroadCastHanders(Candidate aCandidate, List<IHandler> aHandlers)
        {
            byte[] baTarget = null;
            foreach (IHandler handler in aHandlers)
                baTarget = handler.Perform(aCandidate, baTarget);
        }//将 aCandidate 交给指定的处理器列表进行处理。
        protected void ApplyHandersToTargets(Target aTarget, List<IHandler> aHandlers)
        {
            PFMFinder aFinder= PFMFinderFactory.CreateFinder(PFMDefinitions.FILEFINDER
                , aTarget);
            Candidate aCandidate = null;
            while (true)
            {
                aCandidate = aFinder.FindNext();
                if (aCandidate != null)
                    BroadCastHanders(aCandidate, aHandlers);
                else
                    break;
            }

        }
        //依据 aTarget 创建 PFMFinder,让 PFMFinder 找到所有的符合条件
         //Candidate,并一一交给 aHandlers 处理(调用 BroadCastHanders)。
    }
}
